import Vue from 'vue'
import App from './App'
//接口文件
import request from './common/j-request/request.js'
import conf from './common/conf.js'
var baseUrl = 'https://www.haoayi.top'
//baseUrl = 'http://120.25.216.53:8999'
// #ifdef H5
baseUrl = '/api'
baseUrl = '/test'
// #endif

request.setConfig({
    baseUrl: baseUrl,
    contentType: 'form',
    debug: true
})
// 请求拦截
request.interceptor.request = (config => {
    // 给data添加全局请求参数uid
    if (config.data.hasOwnProperty('loadindTip')) {
        config.loadingTip = config.data.loadingTip
        delete config.data.loadingTip
    }
    if (config.data.hasOwnProperty('business')) {
        config.business = config.data.business
        delete config.data.business
    }
    // 给header添加全局请求参数token
    if (!config.header.sessionkey && global.sessionkey) {
        config.header.sessionkey = global.sessionkey
    }
    if (global.sessionkey && !config.data.sessionkey) {
        config.data.sessionkey = global.sessionkey;
    }
    if (global.role && !config.data.type) {
        //config.data.type = global.role;
    }
    // 添加一个自定义的参数，默认异常请求都弹出一个toast提示
    if (config.toastError === undefined) {
        config.toastError = true
    }
    if (config.url.indexOf('userbasic') > 0) {
        config.toastError = false
    }
    return config;
})
// 全局的业务拦截
request.interceptor.response = ((res, config) => {
    if (res.code === '10001' || res.status === '10001') {
        res.success = true;
    } else if (res.code === '10020') {
        // token失效，需要重新登录
        uni.showToast({
            title: '请先登录',
            duration: 2000,
            icon: 'none'
        })
        uni.navigateTo({
            url: '/pages/login/index'
        })
    } else if (res.code === '10030') {

    } else if (res.code === '10040') {
        uni.navigateTo({
            url: '../c/p-credentials'
        })
    }
    return res;
})

// 全局的错误异常处理
request.interceptor.fail = ((res, config) => {
    let ret = res;
    let msg = ''
    if (res.statusCode === 200) { // 业务错误
        msg = res.data.msg
        ret = res.data
    } else if (res.statusCode > 0) { // HTTP错误
        msg = '服务器异常[' + res.statusCode + ']'
    } else { // 其它错误
        msg = res.errMsg
    }
    if (config.toastError) {
        uni.showToast({
            title: msg,
            duration: 2000,
            icon: 'none'
        })
    }
    return ret;
})
Vue.config.productionTip = false

global.devEnv = process.env.NODE_ENV === 'development'
global.request = request

Vue.prototype.$global = global
Vue.prototype.$request = request
global.$toast = function(msg) {
    uni.showToast({
        title: msg,
        duration: 2000,
        icon: 'none'
    })
}

global.$goInfo = function(role) {
    let url = ''
    if (role == 1) {
        global.$toast('请先填写简历')
        url = '../c/p-resume'
    } else if (role == 2) {
        global.$toast('请先填写基本信息')
        url = '../yz/yz-info'

    } else if (role == 3) {
        global.$toast('请先完善您的个人信息')
        url = '../gs/gs-info'
    }
    if (url) {
        setTimeout(function() {
            uni.navigateTo({
                url: url
            })
        }, 500)
    }
}
Vue.prototype.$toast = global.$toast
Vue.prototype.$goInfo = global.$goInfo

// 身份状态检查
Vue.prototype.$checkIdentification = function(needStatus = 20) {
    let role = global.role
    let curStatus = global.identification || 0
    console.log(`role ${role} current status is ${curStatus}, need status is ${needStatus}`);
    // #ifdef H5
    return true;
    // #endif
    if (global.devEnv) {
        //return true;
    }
    if (!global.sessionkey) {
        console.warn('未登录，需要先登录');
        uni.navigateTo({
            url: '../login/index'
        })
        return;
    }
    if (curStatus < needStatus) {
        console.log('status not match to need');
        if (needStatus == 10) { //需登录
            uni.navigateTo({
                url: '../login/index',
                success: (res) => {
                    console.log(res);
                },
                fail: (res) => {
                    console.error(res);
                }
            })
        } else if (needStatus == 20) { //需绑定手机
            uni.navigateTo({
                url: '../login/index'
            })
        } else if (needStatus == 30) { //个人信息
            if (curStatus < 20) {
                return this.$checkIdentification(20)
            }
            this.$goInfo(role)

        } else if (needStatus == 40) { //资质认证
            if (curStatus < 30) {
                this.$toast('')
                return this.$checkIdentification(30)
            }
            wx.showModal({
                content: '请完成资质认证',
                showCancel: false,
                success: function(res) {
                    if (res.confirm) {
                        // TODO
                        uni.navigateTo({
                            url: '../c/p-credentials'
                        })
                    }
                }
            });
        }
        return false;
    }
    return true;
}
Vue.prototype.$setRole = function(role) {
    console.log('set role to ' + role);
    global.role = role;
    uni.setStorageSync('role', role);
}
Vue.prototype.$setOpenid = function(openid) {
    console.log('set openid to ' + openid);
    global.openid = openid;
    uni.setStorageSync('openid', openid);
}
Vue.prototype.$setSessionKey = function(sessionkey) {
    console.log('set sessionkey to ' + sessionkey)
    global.sessionkey = sessionkey
    uni.setStorageSync('sessionkey', sessionkey)
}
Vue.prototype.$setIdentification = function(identification) {
    console.log('set identification to ' + identification)
    global.identification = identification
    uni.setStorageSync('identification', identification)
}
Vue.prototype.$getUserBasic = async function() {
    if (global.openid && global.role) {
        let res = await request.post({
            url: '/user/userbasic',
            data: {
                openid: global.openid,
                type: global.role
            }
        });
        global.userbasic = res;
        uni.setStorageSync('userbasic', res)
        if (!res) {
            // TODO 
        }
        if (res) {
            if (res.identification > global.identification) {
                this.$setIdentification(res.identification)
            }
            let white = ['15312074761', '13913316745', '18994078688', '15295778493']//, '13951004806'
            //white = []
            if (white.indexOf(res.phone) >= 0  && res.sessionkey) {
                this.$setSessionKey(res.sessionkey)
            }
        }
        
        return res;
    }
    return null;
}

Vue.prototype.$getJobType = function(type) {
    if (type > 0 && type < conf.ayTypes.length) {
        return conf.ayTypes[type];
    }
    return '';
}

Vue.prototype.$getJobStatus1 = function(type) {
    type = parseInt(type)
    if (type >= 0 && type < conf.jobState.length) {
        return conf.jobState[type];
    }
    return '';
}
Vue.prototype.$getJobStatus2 = function(type) {
    type = parseInt(type)
    if (type > 0 && type < conf.jobState2.length) {
        return conf.jobState2[type];
    }
    return '';
}
Vue.prototype.$getJobStatus = function(status1, status2) {
    if (status2) {
        return this.$getJobStatus2(status2)
    }
    return this.$getJobStatus1(status1)
}
Vue.prototype.$getAuntyStatus1 = function(type) {
    type = parseInt(type)
    if (type >= 0 && type < conf.auntyState1.length) {
        return conf.auntyState1[type];
    }
    return '';
}
Vue.prototype.$getAuntyStatus2 = function(type) {
    type = parseInt(type)
    if (type > 0 && type < conf.auntyState2.length) {
        return conf.auntyState2[type];
    }
    return '';
}
Vue.prototype.$getAuntyStatus = function(status1, status2) {
    if (status2) {
        return this.$getAuntyStatus2(status2)
    }
    return this.$getAuntyStatus1(status1)
}

Vue.prototype.$isMobile = function(phone) {
    return /^1[3-9]\d{9}$/.test(phone)
}
App.mpType = 'app'

const app = new Vue({
    ...App
})
app.$mount()
